package org.huzhp.security;

import org.huzhp.service.IHuzhpUserService;
import org.springframework.context.MessageSource;
import org.springframework.context.MessageSourceAware;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.security.core.SpringSecurityMessageSource;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.util.Assert;

import java.util.Map;

public class BaseUserDetailService implements UserDetailsService, MessageSourceAware {


    protected MessageSourceAccessor messages = SpringSecurityMessageSource.getAccessor();

    private IHuzhpUserService huzhpUserService;

    public BaseUserDetailService(IHuzhpUserService huzhpUserService) {
        this.huzhpUserService = huzhpUserService;
    }

    @Override
    public void setMessageSource(MessageSource messageSource) {
        Assert.notNull(messageSource, "messageSource cannot be null");
        this.messages = new MessageSourceAccessor(messageSource);

    }

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        Map<String, String> user = huzhpUserService.findUserByCode(username);
        return new OauthUser(user.get("code"), user.get("name"), user.get("password"), AuthorityUtils.NO_AUTHORITIES);
    }
}
